package com.yunhe.authority.util;

import com.yunhe.authority.domain.authority.BackUser;
import com.yunhe.authority.domain.authority.Firm;
import com.yunhe.authority.service.authority.BackUserService;
import com.yunhe.common.constant.SystemConsts;
import com.yunhe.authority.domain.authority.Station;
import com.yunhe.authority.domain.authority.User;
import com.yunhe.authority.service.authority.FirmService;
import com.yunhe.authority.service.authority.StationService;
import com.yunhe.authority.service.authority.UserService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

/**
 * 请求头工具类
 * @author liuronglei
 */

@Component
public class MyHeaderUtil {
    @Autowired
    private UserService userService;
    @Autowired
    private FirmService firmService;
    @Autowired
    private StationService stationService;
    @Autowired
    private BackUserService backUserService;

    public User getHeaderUser() {
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        String userIdStr = request.getHeader(SystemConsts.HEADER.USER_ID.value());
        if (StringUtils.isEmpty(userIdStr)) {
            return null;
        }
        Long userId = Long.parseLong(userIdStr);
        return userService.findByUserId(userId);
    }

    public Station getHeaderStation() {
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        String stationIdStr = request.getHeader(SystemConsts.HEADER.STATION_ID.value());
        if (StringUtils.isEmpty(stationIdStr)) {
            return null;
        }
        Long stationId = Long.parseLong(stationIdStr);
        return stationService.findById(stationId);
    }

    public Firm getHeaderFirm() {
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        String firmIdStr = request.getHeader(SystemConsts.HEADER.FIRM_ID.value());
        if (StringUtils.isEmpty(firmIdStr)) {
            return null;
        }
        Long firmId = Long.parseLong(firmIdStr);
        return firmService.findByFirmId(firmId);
    }

    public Long getHeaderUserId() {
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        String userIdStr = request.getHeader(SystemConsts.HEADER.USER_ID.value());
        if (StringUtils.isEmpty(userIdStr)) {
            return null;
        }
        Long userId = Long.parseLong(userIdStr);
        return userId;
    }

    public BackUser getHeaderBackUser() {
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        String userIdStr = request.getHeader(SystemConsts.HEADER.USER_ID.value());
        if (StringUtils.isEmpty(userIdStr)) {
            return null;
        }
        Long userId = Long.parseLong(userIdStr);
        return backUserService.findByUserId(userId);
    }
}
